const express = require("express");
const router = express.Router();
const connection = require("../db/sql");

// 病人信息，默认返回所有病人信息
router.get("/", function (req, res, next) {
  let limit = 0;
  if (req.query.limit) {
    limit = req.query.limit;
  }
  let pageSql = `select * from patient where dataState=1 limit ${
    limit * 10
  },10`;
  let countSql = "select count(*) from patient";
  connection.query(pageSql, (err, data) => {
    console.log(data);
    connection.query(countSql, (err, count) => {
      if (err) throw err;
      res.json({
        code: 200,
        count: count[0]["count(*)"],
        data,
      });
    });
  });
});

// 病人数据分类筛选，支持分页筛选     1：待就诊 2：已就诊 3：已取消
router.get("/select", function (req, res, next) {
  let limit = 0;
  let selectQuery = 1;
  if (req.query.selectQuery) {
    selectQuery = req.query.selectQuery;
  }
  let selectSql = `select *  from registrationtype a,patient b where a.registrationStatus=b.registrationStatus and a.rtid=${selectQuery} limit ${
    limit * 10
  },10`;
  connection.query(selectSql, (err, data) => {
    if (err) throw err;
    res.json({
      code: 200,
      data,
    });
  });
});

// 病人搜索
router.get("/searchPatient", function (req, res, next) {
  let keyWord = req.query.keyWord;
  let sql = "select * from patient where dataState=1";
  connection.query(sql, (err, data) => {
    let newData = data.filter((item) => {
      if (!keyWord) {
        return item;
      }
      return item.patientName.includes(keyWord);
    });
    if (err) throw err;
    res.json({
      code: 200,
      data: newData,
    });
  });
});

module.exports = router;
